package com.apobates.forum.toro.template.strategy;

import com.apobates.forum.toro.BatchQueryFace;
import com.apobates.forum.toro.template.TemplateAffixStatements;
import com.apobates.forum.toro.utils.BeanHelper;
import org.apache.commons.lang3.tuple.Triple;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.stream.Collectors;

public class TemplateSerialBatchAffixStrategy<E,T> extends AbstractTemplateAffixStrategy<E> implements TemplateBatchAffixStrategy<E,T>{
    private final static Logger logger = LoggerFactory.getLogger(TemplateSerialBatchAffixStrategy.class);
    @Override
    public Collection<E> apply(
            Class<E> targetObjClass,
            Collection<T> basicInsSet,
            List<TemplateAffixStatements> statements,
            Map<String, BatchQueryFace> queries) {
        // 指定tag关联的对象集合，例: 集合中authorId连接哪些Author对象
        Map<String, Collection<?>> queryArgMap = new HashMap<>();
        // 指定tag的连接关系
        Map<String, Triple<String,String,String>> joinAttrMap = new HashMap<>();
        for(TemplateAffixStatements ts : statements){
            // 查询参数
            Set<String> argVal = basicInsSet.stream().map(ele -> BeanHelper.getAttrVal(ele, ts.getJoinAttr()).toString()).collect(Collectors.toSet());
            queryArgMap.put(ts.getNames(), queries.get(ts.getNames()).query(argVal));
            joinAttrMap.put(ts.getNames(), Triple.of(ts.getAffix(),ts.getJoinAttr(), ts.getLinkAttr()));
        }
        // 最终的结果集
        List<E> data = new ArrayList<>();
        // 获取查询结果
        for (T ins : basicInsSet) {
            E tmp = BeanHelper.buildProxyInstance(targetObjClass, ins);
            if (null == tmp) {
                logger.debug(String.format("[Template-Affix][Batch]计算%s类型对象失败", tmp.getClass().getSimpleName()));
                continue;
            }
            /* 给tmp赋值聚合属性值
            Set<String> tagSet = joinAttrMap.keySet();
            // 1: 遍历关联对象绑定的tag
            for(String tag : tagSet) {
                Triple<String,String,String> t = joinAttrMap.get(tag);
                // 2: 前缀affix
                final String affix = t.getLeft();
                // 3: 当前要设置的属性集合
                Set<String> setAffixAttrs = getAffixAttrSet(targetObjClass, affix);
                // 4: 从哪个对象中取得要设置的属性, ins中的哪个属性可以作到
                Object relateArgVal = BeanHelper.getAttrVal(ins, t.getMiddle());
                Optional<?> relateObjIns = seekCollectionEle(relateArgVal, t.getRight(), queryArgMap.get(tag));
                if (!relateObjIns.isPresent()){
                    continue;
                }
                Map<String,Object> tsObjAttrValMap = getGroupObjAttrVal(relateObjIns.get(), affix, setAffixAttrs);
                // 5: 设置对象的指定属性值集合
                setTargetMultiAttrVal(tmp, tsObjAttrValMap);
            }*/
            bindBatchMultiAttr(joinAttrMap, targetObjClass, tmp, ins, queryArgMap);
            data.add(tmp);
        }
        return data;
    }

    @Override
    protected String getSentinel() {
        return "Batch";
    }
}
